home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / SAT 2.4.0 / SAT / Demo ƒ / HeartQuest demo ƒ / sPlayer.p < prev    next >
Encoding:
Text File  |  1997-02-13  |  4.1 KB  |  168 lines  |  [TEXT/PJMM]

  1. {===============================================}
  2. {================= Player sprite unit ================}
  3. {===============================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. { This file is the first of several sprite units, units that holds the full}
  10. {description of the objects to be animated. }
  11.  
  12. unit sPlayer;
  13.  
  14. { Sprite unit. A sprite unit should include the following routines:}
  15. { Init-procedure, that initializes private bitmaps}
  16. { Setup-procedure, that sets variables other than the standard ones set by MakeSprite }
  17. { Handle-procedure, to be called once per iteration until the sprite dies }
  18. { Hittask-procedure (optional), for advanced collission handling. }
  19.  
  20. { This is the sprite unit for the player object, in this case a butterfly. }
  21.  
  22. interface
  23.  
  24.     uses
  25. {$IFC UNDEFINED THINK_PASCAL}
  26.         Types, Quickdraw, Events, 
  27. {$ENDC}
  28.         GameGlobals, SAT, sHeart, sFlypaper, scores, SoundConst;
  29.  
  30.     var
  31.         stillRunning: boolean;
  32.  
  33.     procedure InitPlayer;
  34.     procedure SetupPlayer (player: SpritePtr);
  35.     procedure HandlePlayer (me: SpritePtr);
  36.     procedure HitPlayer (me, him: SpritePtr);
  37.  
  38. implementation
  39.  
  40.     const
  41. {playerspeed = 16; { Might become a variable one day }
  42.         shift = 4;
  43.         kMaxPlayerFace = 5; {one less than number of player faces}
  44.  
  45.     var
  46.         playerFace: array[0..kMaxPlayerFace] of FacePtr;
  47.         posh, posv: longint;
  48.  
  49.     procedure InitPlayer;
  50.         var
  51.             ii: integer;
  52.     begin
  53.         for ii := 0 to kMaxPlayerFace do
  54.             begin
  55.                 playerFace[ii] := SATGetFace(140 + ii);
  56.             end;
  57.     end;
  58.  
  59.     procedure SetupPlayer (player: SpritePtr);
  60.     begin
  61.         player^.face := playerFace[0];
  62.  
  63. {The butterfly is 58x31 pixels. Only the middle counts!}
  64.         SetRect(player^.hotRect, -15 + 29, 0, 15 + 29, 31);
  65.  
  66.         posh := bsl(player^.position.h, shift);
  67.         posv := bsl(player^.position.v, shift);
  68.         slowcount := 0;
  69.         player^.task := @HandlePlayer;
  70.         player^.hittask := @HitPlayer;
  71.     end;
  72.  
  73.  
  74.     procedure HandlePlayer (me: SpritePtr);
  75.         var
  76.             pt: point;
  77.     begin
  78.         if me^.kind <> 2 then
  79.             me^.kind := 2;
  80.  
  81.         if slowcount > 0 then
  82.             begin
  83.                 slowcount := pred(slowcount);
  84.                 me^.speed := Point(0);
  85. {SetMouse(Point($00AB0100));???? {256,171}
  86.             end
  87.         else
  88.             begin
  89. {Note about the controls:}
  90. {(posh,posv) is the position scaled up by 16 (2**shift)}
  91. {This is my way to implement fixed-point arithmetics efficiently.}
  92. {If we used only integers, we wouldn't get the smooth movement the}
  93. {bufferfly has.}
  94.                 SetPort(gSAT.wind.port);
  95.                 GetMouse(pt);
  96.  
  97.                 if pt.h < 0 then
  98.                     pt.h := 0;
  99.                 if pt.h > 512 then
  100.                     pt.h := 512;
  101.                 if pt.v < 0 then
  102.                     pt.v := 0;
  103.                 if pt.v > 342 then
  104.                     pt.v := 342;
  105.  
  106.                 me^.speed.h := 15 * me^.speed.h div 16 + pt.h - 256;
  107.                 me^.speed.v := 15 * me^.speed.v div 16 + pt.v - 171;
  108.  
  109. {If we rather want the direct velocity-from-position that Crystal Quest has,}
  110. {we can use the following:}
  111. {me^.speed.h := pt.h - 256;}
  112. {me^.speed.v := pt.v - 171;}
  113.  
  114.                 SATSetMouse(Point($00AB0100));{256,171}
  115.  
  116.                 posh := posh + me^.speed.h;
  117.                 posv := posv + me^.speed.v;
  118.  
  119.                 me^.position.h := bsr(posh, shift);
  120.                 me^.position.v := bsr(posv, shift);
  121.  
  122.                 if me^.position.h > gSAT.offSizeH - xsize then
  123.                     begin
  124.                         me^.position.h := gSAT.offSizeH - xsize; { ev. xsize - 10? }
  125.                         posh := bsl(gSAT.offSizeH - xsize, shift);
  126.                     end;
  127.                 if me^.position.h < 0 then
  128.                     begin
  129.                         me^.position.h := 0;
  130.                         posh := 0;
  131.                     end;
  132.                 if me^.position.v > gSAT.offSizeV - 32 then
  133.                     begin
  134.                         me^.position.v := gSAT.offSizeV - 32; { ev. xsize - 10? }
  135.                         posv := bsl((gSAT.offSizeV - 32), shift);
  136.                     end;
  137.                 if me^.position.v < 0 then
  138.                     begin
  139.                         me^.position.v := 0;
  140.                         posv := 0;
  141.                     end;
  142.             end; { if slowcount... }
  143.  
  144.         me^.mode := me^.mode + 1;
  145.         if slowcount = 0 then
  146.             begin
  147.                 if me^.mode > 10 then
  148.                     begin
  149.                         me^.mode := 0;
  150.                     end;
  151.                 me^.face := playerFace[abs(me^.mode - 5)];
  152.             end
  153.         else
  154.             begin
  155.                 me^.face := playerFace[band(slowcount, 1) * 3];
  156.                 if slowcount = 1 then
  157.                     SATSoundPlay(splatt3SndH, 1, true);
  158.             end;
  159.  
  160.         playerPos := me^.position;
  161.     end;
  162.  
  163.     procedure HitPlayer (me, him: SpritePtr);
  164.     begin
  165. {This would be an alternative place to handle collisions. Not used in this program.}
  166.     end;
  167.  
  168. end.